package es.mgil.whoWantsToBeAMillonaire.runnables;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import android.content.Context;
import android.os.Message;
import es.mgil.whoWantsToBeAMillonaire.handlers.SendScoreHandler;

public class SendScoreRunnable implements Runnable {
	public static final int SEND_SCORE_ERROR_NAME = 0;
	public static final int SEND_SCORE_ERROR_COORDINATES = 1;
	public static final int SEND_SCORE_ERROR_SERVER = 2;
	public static final int SEND_SCORE_OK = 3;

	private SendScoreHandler handler;
	private String name;
	private float longitude;
	private float latitude;
	private int score;

	public SendScoreRunnable(Context context, String name, float longitude,
			float latitude, int score) {
		this.name = name;
		this.longitude = longitude;
		this.latitude = latitude;
		this.score = score;
		this.handler = new SendScoreHandler(context);
	}

	@Override
	public void run() {
		try {
			// create the parameters for the connection, indicating a timeout
			HttpParams params = new BasicHttpParams();
			HttpConnectionParams.setConnectionTimeout(params, 3000);
			HttpConnectionParams.setSoTimeout(params, 5000);

			// create the connection to the external service
			HttpClient client = new DefaultHttpClient(params);
			HttpPut request = new HttpPut(
					"http://soletaken.disca.upv.es:8080/CMU/rest/highscores");
			List<NameValuePair> pairs = new ArrayList<NameValuePair>();
			pairs.add(new BasicNameValuePair("name", name));
			pairs.add(new BasicNameValuePair("longitude", String
					.valueOf(longitude)));
			pairs.add(new BasicNameValuePair("latitude", String
					.valueOf(latitude)));
			pairs.add(new BasicNameValuePair("score", String.valueOf(score)));
			request.setEntity(new UrlEncodedFormEntity(pairs));
			client.execute(request);
			handler.sendMessage(Message.obtain(handler, SEND_SCORE_OK));
		} catch (ClientProtocolException e) {
			// an error occurred while connecting to the server
			handler.sendMessage(Message
					.obtain(handler, SEND_SCORE_ERROR_SERVER));
		} catch (UnsupportedEncodingException e) {
			// an error occurred while connecting to the server
			handler.sendMessage(Message
					.obtain(handler, SEND_SCORE_ERROR_SERVER));
		} catch (IOException e) {
			// an error occurred while connecting to the server
			handler.sendMessage(Message
					.obtain(handler, SEND_SCORE_ERROR_SERVER));
		}
	}

	/**
	 * Send the won score to the server
	 */
	public void sendScore() {
		if (name.trim().equals("")) {
			// only do it if a name is set
			handler.sendMessage(Message.obtain(handler, SEND_SCORE_ERROR_NAME));
		} else if (longitude == 0 || latitude == 0) {
			// only do it if the location is set
			handler.sendMessage(Message.obtain(handler,
					SEND_SCORE_ERROR_COORDINATES));
		} else {
			Thread thread = new Thread(this);
			thread.start();
		}
	}
}
